package com.my.fo.strategy.organization.type;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.my.fo.strategy.organization.inter.OrganizationExistTypeStrategy;
import com.my.fo.strategy.organization.inter.OrganizationTypeStrategy;
import com.my.fo.strategy.interfaces.StrategyFactoryName;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Files;

@StrategyFactoryName("copy")
@Slf4j
public class CopyOrganizationTypeStrategy implements OrganizationTypeStrategy {

    @Override
    public boolean handlerType(String inputSrc, String outputSrc, OrganizationExistTypeStrategy existTypeStrategy) {
        return handlerType(new File(inputSrc), new File(outputSrc), existTypeStrategy);
    }

    @Override
    public boolean handlerType(File inputFile, File outputFile, OrganizationExistTypeStrategy existTypeStrategy) {
        try {
            if (existTypeStrategy.handlerExist(inputFile, outputFile) && !FileUtil.equals(inputFile, outputFile)) {
                return ObjectUtil.isNotNull(FileUtil.copy(inputFile, outputFile, true));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new RuntimeException("【复制】文件整理发生错误：", e);
        }
        return true;
    }

}
